import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

import com.dtu.distributed.connection.AdminConnection;
import com.dtu.distributed.exception.NotAdminException;

public class Client {
	
	public static double getAvg(String host, int port) {
		
		String rmiRegistryName = host + ":" + port;
		System.out.println("RMI:" + rmiRegistryName);
		try {
			Registry registry = LocateRegistry.getRegistry(host);
			AdminConnection admin = (AdminConnection) registry.lookup(rmiRegistryName);
			return admin.getAvg();
		}
		catch (NotAdminException e) {
			System.out.println(e.getMessage());
			//e.printStackTrace();
		}
		catch (Exception e) {
			System.out.println("Connection could not be established. Try Again...");
			//e.printStackTrace();
		}
		return -1; // TODO: should not return 0
	}
	
	public static String shiftAdmin(String host, int port) {
		
		String rmiRegistryName = host + ":" + port;
		
		try {
			Registry registry = LocateRegistry.getRegistry(host);
			AdminConnection admin = (AdminConnection) registry.lookup(rmiRegistryName);
			return "new admin's identifier: " +admin.shiftAdmin();
		} catch (Exception e) {
			return "Connection could not be established. Try Again...";
		}
		
	}
		
}
